import './index.scss';
export default ngModule => {
    ngModule.directive('selfTable', ['$compile', '$timeout', function ($compile, $timeout) {
        return {
            restrict: 'E',
            template: require('./index.html'),
            scope: {
                columns: '=',
                data: '='
            },
            link: function (scope, element, attr) {
                scope.mainCtr = {
                    isCheckedAll: false,
                    checkedAll() {
                        $timeout(() => {
                            _.forEach(scope.data, item => {
                                item.checked = this.isCheckedAll;
                            });
                        });
                    },
                    checkedItem() {
                        $timeout(() => {
                            let flag = true;
                            _.forEach(scope.data, item => {
                                if (!item.checked) {
                                    flag = false;
                                    return false;
                                }
                            });
                            this.isCheckedAll = flag;
                        })
                    },
                    renderBody() {
                        _.forEach(this.data, d => {
                            _.forEach(this.columns, c => {
                                let { key } = c;
                                if (!d[key]) {
                                    if (c.render) {
                                        d[key] = $compile(c.render())(scope);
                                    } else if (c.formatter) {
                                        d[key] = c.formatter;
                                    }
                                }
                            })
                        });
                    },
                    /**
                     * 绑定每个head中的方法，注意方法不能重名，否则会出现覆盖问题
                     */
                    bindOperate() {
                        this.operate = {};
                        _.forEach(this.columns, item => {
                            if (item.operate) {
                                _.forEach(item.operate, subItem => {
                                    this.operate[subItem.name] = subItem;
                                });
                            }
                        })
                    },
                    sortKey: '',
                    sortOrder: '',
                    sortClass: '',
                    lastIndex: 0,
                    handleSort(key, index) {
                        if (key !== this.sortKey) {
                            this.sortOrder = '';
                        }
                        this.sortKey = key;
                        this.columns[this.lastIndex].sortClass = '';
                        this.lastIndex = index;
                        switch (this.sortOrder) {
                            case '':
                                this.sortOrder = 'asc';
                                this.columns[this.lastIndex].sortClass = 'up';
                                break;
                            case 'asc':
                                this.sortOrder = 'desc';
                                this.columns[this.lastIndex].sortClass = 'down';
                                break;
                            case 'desc':
                                this.sortOrder = '';
                                this.columns[this.lastIndex].sortClass = '';
                                break;
                        }
                        scope.$emit('getTableData', { sortKey: this.sortKey, sortOrder: this.sortOrder });
                    },
                    init() {
                        this.showCheckbox = scope.columns[0].showCheckbox;
                        this.data = scope.data;
                        this.columns = scope.columns.slice(1);
                        this.bindOperate();
                        this.renderBody();
                    }
                }
                scope.mainCtr.init()
            }
        }
    }]);
    ngModule.directive('cellDiv', ['$compile', function ($compile) {
        return {
            restrict: 'E',
            template: `<div class="cell-div"></div>`,
            scope: {
                render: '='
            },
            replace: true,
            link(scope, element, attr) {
                element.html(scope.render);
            }
        }
    }])
}